package org.rqb.panel;

import org.rqb.Constant;
import org.rqb.config.MyConfig;
import org.rqb.frame.MyFrame;
import org.rqb.utils.HttpUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.HashMap;
import java.util.Properties;

import static org.rqb.Constant.*;

public class ProxySettingPanel extends JPanel implements ActionListener {

    private MyFrame myFrame;

    private Properties config = MyConfig.getConfig();

    private JCheckBox enableProxyCheckBox;
    private JTextField hostField;
    private JTextField portField;
    private JTextField usernameField;
    private JPasswordField passwordField;
    private JButton saveButton;
    private JButton cancelButton;

    public ProxySettingPanel(MyFrame myFrame) {
        this.myFrame = myFrame;
        initComponents();
        loadConfig();
    }

    private void initComponents() {
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 10, 5, 10);
        gbc.fill = GridBagConstraints.HORIZONTAL;

        enableProxyCheckBox = new JCheckBox("启用代理");
        hostField = new JTextField(20);
        portField = new JTextField(6);
        usernameField = new JTextField(15);
        passwordField = new JPasswordField(15);
        saveButton = new JButton("保存");
        cancelButton = new JButton("取消");

        gbc.gridx = 0;
        gbc.gridy = 0;
        add(enableProxyCheckBox, gbc);

        gbc.gridy++;
        add(new JLabel("代理地址：" ), gbc);
        gbc.gridx = 1;
        add(hostField, gbc);

        gbc.gridx = 0;
        gbc.gridy++;
        add(new JLabel("端口：" ), gbc);
        gbc.gridx = 1;
        add(portField, gbc);


        JPanel buttonPanel = new JPanel();
        buttonPanel.add(saveButton);
        buttonPanel.add(cancelButton);

        gbc.gridx = 0;
        gbc.gridy++;
        gbc.gridwidth = 2;
        add(buttonPanel, gbc);


        saveButton.addActionListener(this);
        cancelButton.addActionListener(this);



    }

    private void loadConfig() {
        boolean enabled = Boolean.parseBoolean(config.getProperty(PROXY_ENABLED, "false"));
        String host = config.getProperty(PROXY_HOST, "");
        String port = config.getProperty(PROXY_PORT, "");

        enableProxyCheckBox.setSelected(enabled);
        hostField.setText(host);
        portField.setText(port);
    }

    private void saveConfig() {
        HashMap<String, String> map = new HashMap<>();
        map.put(PROXY_ENABLED, String.valueOf(enableProxyCheckBox.isSelected()));
        map.put(PROXY_HOST, hostField.getText().trim());
        map.put(PROXY_PORT, portField.getText().trim());

        MyConfig.updateConfig(map);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() instanceof JButton) {
            JButton button = (JButton) e.getSource();
            switch (button.getText()) {
                case "保存" :
                    //保存配置
                    saveConfig();
                    //取消对话框
                    SwingUtilities.getWindowAncestor(ProxySettingPanel.this).dispose();
                    //更新代理对象
                    updateProxy();

                    break;
                case "取消" :
                    // 可以关闭对话框
                    SwingUtilities.getWindowAncestor(ProxySettingPanel.this).dispose();
                    break;
            }



        }




    }

    /**
     * 更新代理配置
     */
    private void updateProxy() {
        Proxy proxy;
        String host = config.getProperty(PROXY_HOST, "");
        String port = config.getProperty(PROXY_PORT, "");
        String proxyEnable = config.getProperty(Constant.PROXY_ENABLED);
        if("true".equals(proxyEnable)) {
            if (host != null && port != null && !host.isEmpty() && !port.isEmpty()) {
                proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host,Integer.parseInt(port)));
            } else {
                proxy = Proxy.NO_PROXY;
            }
        }else {
            proxy = Proxy.NO_PROXY;
        }
        HttpUtil.updateProxy(proxy);
    }


}
